package com.xinzhi.service.impl;


import cn.hutool.core.date.DateUtil;
import com.xinzhi.Utils.Result;
import com.xinzhi.entity.*;
import com.xinzhi.entity.need.OrderRequest;
import com.xinzhi.mapper.ApplicantMapper;
import com.xinzhi.mapper.ManMapper;
import com.xinzhi.mapper.OrderMapper;
import com.xinzhi.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

@Service
public class OrderService implements com.xinzhi.service.OrderService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ApplicantMapper applicantMapper;

    @Autowired
    private ManMapper manMapper;


    /**
     * 生成订单以及订单详情
     * @param list
     * @return
     */
    @Transactional
    @Override
    public Result newOrder(List<OrderRequest> list) {
        Orders order = new Orders();
        //---------------------------
        Random a= new Random();
        int water = a.nextInt(1000000000)+10000000;//生成订单号
        String now = DateUtil.now();//生成订单时间

        int sum  = 0;
        BigDecimal allPrice = new BigDecimal("0");  // 总价

        for (OrderRequest orderRequest : list) {
            Orders_detail orders_detail = new Orders_detail();
            //--------------------------------------
            Integer userId = orderRequest.getUserId();
            Integer productId = orderRequest.getProductId();
            Integer num = orderRequest.getNum();
            Product product = productMapper.selectPriceByGoodsId(productId);
            sum = sum+num;
            //--------------订单详情表------------------
            orders_detail.setProductId(productId);
            orders_detail.setNumber(num);
            orders_detail.setOrdersId(water+"");
            //------------返回页面需要的参数------------
            //自提人信息和自提点
            order.setManId(orderRequest.getManId());//接收到传过来的自提人id，存到订单表-7
            order.setPlaceId(orderRequest.getPlaceId());//接收到传来的自提点id，存到订单表-8
            Applicant applicant = applicantMapper.showPoint();//拿到自提点
            List<Man> men = manMapper.showManInfo(orderRequest.getUserId());

            //-----------------------数量单价x数量 得到总价----------//从参数接收到的数量，转型成Big
            BigDecimal number=BigDecimal.valueOf((orderRequest.getNum()));
            BigDecimal multiply = product.getPrice().multiply(number);
            //用BigDecimal的乘法方式计算总价
            orders_detail.setTotal(multiply);
            orderMapper.newDetails(orders_detail);
            allPrice = allPrice.add(multiply);
            System.out.println(allPrice);
            order.setUserId(userId);//用户id-6
            //提货人+提货点
            System.out.println("结束循环");
        }
        order.setOrderNum(water+"");//本次订单号-1
        order.setOrderTime(now);//本次订单生成时间-2
        order.setState(0);//订单状态-3
        order.setNum(sum);//商品总数量-5
        order.setTotal(allPrice);//订单总价---4
        orderMapper.generate(order);
        if (order.getUserId() != null)
        {
            Result.ok(""+"订单创建成功,订单号是"+water);
        }
        return Result.error(200,"订单创建失败");

    }
}

//        User user =(User) request.getSession().getAttribute("User");
//        userId = user.getId();
//获取所有键的集合。用keySet()方法实现
//        Set<String> keySet = products.keySet();
//----------------------set订单------------------------
//遍历键的集合，获取到每一个键。用增强for实现
//        for (String k:keySet) {
//根据键去找值。用get(Object key)方法实现
//            String value = products.get(k);
//        }

//        long l = IdUtil.getSnowflake(1, 20).nextId();
//        Integer water = Integer.parseInt(String.valueOf(l));
